﻿Imports MySql.Data.MySqlClient
Public Class frmedit_voyage
    Dim mysql As MySqlConnection = main_form.mysqlconection
    Dim mysql1 As MySqlConnection = main_form.mysqlconection1

    Dim mySqlCommand As New MySqlCommand
    Dim mySqlAdaptor As New MySqlDataAdapter
    Dim mySqlReader As MySqlDataReader


    Dim mySqlCommand1 As New MySqlCommand
    Dim mySqlAdaptor1 As New MySqlDataAdapter
    Dim mySqlReader1 As MySqlDataReader
    Dim respone As Object
    Dim idvoyn As String
    Dim idvoys As String


    Dim datepenang As String
    Dim datethai As String
    Dim timehhmm As String
    Dim datetime As String
    Dim sumint As Double

    Dim textDate As String
    Dim stringDate() As String
    Dim stringhour() As String
    Dim sumtxt As String

    Private Sub frmedit_voyage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
        System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo("en-US")


        txt_voyage.Text = Format(frmsearch_voyage.idvoyage, "000")

        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If
        mySqlCommand.CommandText = "Select VOYVESNAMEN,VSSNATION,VSSMASTER,VOYDATESN,VOYDATEEN,VOYVESIDN,VSSNAM,VOYTIMEHHMMNN from voyage join vesmain on voyage.VOYVESIDN = vesmain.VESMAINID where VOYAGEID ='" & frmsearch_voyage.idvoyage & "' ;"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())


                'date_as = mySqlReader("VOYDATESN")
                'date_eta_penang.Value = date_asmySqlReader("VOYDATESN")
                'date_eta_penang.Text = mySqlReader("VOYDATESN").ToString("dd-MM-yyyy")
                ' 'Dim myDate As DateTime = DateTime.Parse(mySqlReader("VOYDATESN") + " 00:00:00")
                ' ''date_eta_penang.Value = myDate
                ''date_eta_penang.CustomFormat = "dd/MM/yyyy"
                idvoyn = mySqlReader("VOYVESIDN")
                date_eta_penang.Text = mySqlReader("VOYDATESN")
                'date_as = "#" + mySqlReader("VOYDATEEN") + " " + "00:00:00#"
                date_etd_penang.Text = mySqlReader("VOYDATEEN")
                ComboBox2.Text = mySqlReader("VSSNAM")
                txt_nationality.Text = mySqlReader("VSSNATION")
                txt_namemaster.Text = mySqlReader("VSSMASTER")

                DateTimePicker4.Text = mySqlReader("VOYTIMEHHMMNN")

            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()


        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If
        mySqlCommand.CommandText = "Select * from voyage join vesmain on voyage.VOYVESIDS = vesmain.VESMAINID where VOYAGEID ='" & frmsearch_voyage.idvoyage & "' ;"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())
                ComboBox1.Text = mySqlReader("VSSNAM")
                txt_nationality2.Text = mySqlReader("VSSNATION")
                txt_namemaster2.Text = mySqlReader("VSSMASTER")
                idvoys = mySqlReader("VOYVESIDS")
                'date_as = "#" + mySqlReader("VOYDATESS") + " " + "00:00:00#"
                'date_krabi_eta.Value = date_as
                date_krabi_eta.Text = mySqlReader("VOYDATESS")
                'date_as = "#" + mySqlReader("VOYDATEES") + " " + "00:00:00#"
                'date_krabi_etd.Value = date_as
                date_krabi_etd.Text = mySqlReader("VOYDATEES")
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try


        mysql.Close()
        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If
        mySqlCommand.CommandText = "Select * from vesmain;"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())
                ComboBox1.Items.Add(mySqlReader("VSSNAM"))
                ComboBox2.Items.Add(mySqlReader("VSSNAM"))
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()
    End Sub

    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click



        timehhmm = date_etd_penang.Text
        datetime = DateTimePicker4.Text

        stringDate = Split(timehhmm, "-")
        'MsgBox(stringDate(0) + stringDate(1) + stringDate(2))
        stringhour = Split(DateTime, ":")
        'MsgBox(stringhour(0) + stringhour(1))
        sumtxt = stringDate(2) + stringDate(1) + stringDate(0) + stringhour(0) + stringhour(1)

        sumint = CDbl(sumtxt) + 100

   
        mysql.Close()
        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If
        respone = MsgBox("ยืนยันข้อมูลถูกต้อง", MsgBoxStyle.OkCancel + MsgBoxStyle.Information, "Warning Messsage")
        If respone = 1 Then
            Dim commandText2 As String
            commandText2 = "UPDATE voyage SET VOYVESIDN = '" & idvoyn & "' ,VOYVESIDS ='" & idvoys & "',VOYDATESN ='" & date_eta_penang.Text & "', VOYDATESS ='" & date_krabi_eta.Text & "',VOYDATEEN ='" & date_etd_penang.Text & "',VOYDATEES = '" & date_krabi_etd.Text & "',VOYVESNAMEN ='" & ComboBox2.Text & "',VOYVESNAMES = '" & ComboBox1.Text & "',VOYTIMEHHMMNN ='" & DateTimePicker4.Text & "' where VOYAGEID = '" & frmsearch_voyage.idvoyage & "'"

            Try
                mySqlCommand.CommandText = commandText2
                mySqlCommand.CommandType = CommandType.Text
                mySqlCommand.Connection = mysql

                mySqlCommand.ExecuteNonQuery()

                mysql.Close()

                If mysql.State = ConnectionState.Closed Then
                    mysql.Open()
                End If

                mySqlCommand.CommandText = "Select * from ctnmain where CTNVOYN = '" & CInt(txt_voyage.Text) & "';"
                ' mySqlCommand.CommandText -0mySqlCommand.Connection = mysql
                mySqlCommand.Connection = mysql
                mySqlAdaptor.SelectCommand = mySqlCommand
                Try
                    mySqlReader = mySqlCommand.ExecuteReader

                    While (mySqlReader.Read())


                        mysql1.Close()
                        If mysql1.State = ConnectionState.Closed Then
                            mysql1.Open()
                        End If
                        Try
                            mySqlCommand1.Parameters.Clear()
                            mySqlCommand1.CommandText = "UPDATE rptwhl join ctnmain on rptwhl.rptidctn = ctnmain.CTNMAINID SET rptwhl.rptctndate = '" & sumtxt + "00" & "',rptwhl.rptdatetype1 ='" & timehhmm & "' , rptwhl.rptctndatetpe ='" & sumint.ToString + "00" & "'where ctnmain.CTNMAINID =  '" & mySqlReader("CTNMAINID") & "';"
                            mySqlCommand1.Connection = mysql1
                                 mySqlCommand1.ExecuteNonQuery()
                            mysql1.Close()
                        Catch ex As Exception
                            MsgBox(ex.ToString)
                        End Try




                        mysql1.Close()

                        mysql1.Close()
                        If mysql1.State = ConnectionState.Closed Then
                            mysql1.Open()
                        End If
                        Try
                            mySqlCommand1.Parameters.Clear()
                            mySqlCommand1.CommandText = "UPDATE ctndepot join ctnmain on ctndepot.ctnid = ctnmain.CTNMAINID SET ctndepot.CTNDEDATES = '" & date_etd_penang.Text & "' where ctnmain.CTNMAINID =  '" & mySqlReader("CTNMAINID") & "';"
                            mySqlCommand1.Connection = mysql1
                            mySqlCommand1.ExecuteNonQuery()
                            mysql1.Close()
                        Catch ex As Exception
                            MsgBox(ex.ToString)
                        End Try




                        mysql1.Close()

                    End While
                Catch ex As Exception
                    MsgBox(ex.ToString)
                End Try
                mysql.Close()



                Dim cf As New frmsearch_voyage

                cf.MdiParent = Me.MdiParent
                Me.Close()
                cf.Dock = DockStyle.Fill
                cf.Show()
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
            mysql.Close()
        End If



    End Sub

    Private Sub ComboBox2_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedValueChanged
        mysql.Close()
        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If

        mySqlCommand.CommandText = "Select * from vesmain where VSSNAM = '" & ComboBox2.Text & "';"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())
                idvoyn = mySqlReader("VESMAINID")
                txt_nationality.Text = mySqlReader("VSSNATION")
                txt_namemaster.Text = mySqlReader("VSSMASTER")
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()
    End Sub

    Private Sub ComboBox1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
        mysql.Close()
        If mysql.State = ConnectionState.Closed Then
            mysql.Open()
        End If
        mySqlCommand.CommandText = "Select * from vesmain where VSSNAM = '" & ComboBox1.Text & "';"
        ' mySqlCommand.CommandText = 
        mySqlCommand.Connection = mysql
        mySqlAdaptor.SelectCommand = mySqlCommand

        Try
            mySqlReader = mySqlCommand.ExecuteReader

            While (mySqlReader.Read())
                idvoys = mySqlReader("VESMAINID")
                txt_nationality2.Text = mySqlReader("VSSNATION")
                txt_namemaster2.Text = mySqlReader("VSSMASTER")
            End While
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        mysql.Close()
    End Sub

    Private Sub ButtonX2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX2.Click
        Dim cf As New frmsearch_voyage

        cf.MdiParent = Me.MdiParent
        Me.Close()
        cf.Dock = DockStyle.Fill
        cf.Show()
    End Sub
End Class